//
//  MyCoreData.m
//  kankanVideo
//
//  Created by wangbo on 5/30/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MyCoreData.h"
#import "AppDelegate.h"
#import <CoreData/CoreData.h>
#import "IndexData.h"
#import "VideoListData.h"
#import "ConfigData.h"

static MyCoreData *coreData;
static NSManagedObjectContext *context;

@implementation MyCoreData
@synthesize delegate;
//@synthesize objects;


+(MyCoreData*)shareInstance{
    if (!coreData) {
        coreData=[[MyCoreData alloc]initWithTableName:@"IndexData"];
        AppDelegate *app=(AppDelegate *)[[UIApplication sharedApplication] delegate];
        context=app.managedObjectContext;
    }
    return coreData;
}

-(id)initWithTableName:(NSString *)tblName{
    NSLog(@"initWithTableName_%@",tblName);
    self=[super init];
    tblname=tblName;
    AppDelegate *app=(AppDelegate *)[[UIApplication sharedApplication] delegate];
    context=app.managedObjectContext;
    return self;
}
-(void)deleteWithParam:(NSMutableArray*)param{
    NSLog(@"delete_classid:%@",[param objectAtIndex:0]);
    for (IndexData *oneObject in [self query])
    {
        if([[oneObject classid] isEqualToString:[param objectAtIndex:0]]){
            [context deleteObject:oneObject];
            break;
        }
    }
    if (![context save:&error]) {
        NSLog(@"the error is %@",[error userInfo]);
    }else{
        NSLog (@"The deletedManagedObject's id is: %@", [param objectAtIndex:0]);
    }
}
-(void)insertVideoList:(NSMutableArray *)param withClassid:(NSString *)clsid{
    for (NSMutableArray* par in param) {
        VideoListData *obj=(VideoListData *)[NSEntityDescription insertNewObjectForEntityForName:@"VideoListData" inManagedObjectContext:context];
        [obj setClassid:clsid];
        [obj setTitle:[par objectAtIndex:0]];
        [obj setTitlepic:[par objectAtIndex:2]];
        [obj setTitleurl:[par objectAtIndex:1]];
    }
    if (![context save:&error]) {
        NSLog(@"the error is %@",[error userInfo]);
    }else{
        NSLog(@"insert successfully!");
        //NSLog (@"The newManagedObject's id is: %@", [obj valueForKey:@"title"]);
    }
}
-(void)deleteVideoList:(NSString *)classid{
    for (VideoListData *oneObject in [self queryVideoList:classid])
    {
        [context deleteObject:oneObject];
    }
    if (![context save:&error]) {
        NSLog(@"the error is %@",[error userInfo]);
    }else{
        NSLog (@"The deletedManagedObject's id is: %@", classid);
    }
}
-(NSArray*)queryVideoList:(NSString *)classid{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"VideoListData" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    //[fetchRequest setFetchBatchSize:20];
    if (![classid isEqualToString:@"all"]) {
        NSPredicate *prediate=[NSPredicate predicateWithFormat:@"(classid = %@)",classid];
        [fetchRequest setPredicate:prediate];
    }
    objects = [context executeFetchRequest: fetchRequest error:&error];
    //NSLog(@"%d",objects.retainCount);
    return objects;
}
-(void)insertWithParam:(NSMutableArray *)param{
    IndexData *obj=(IndexData *)[NSEntityDescription insertNewObjectForEntityForName:tblname inManagedObjectContext:context];
    [obj setClassid:[param objectAtIndex:0]];
    [obj setTitle:[param objectAtIndex:1]];
    [obj setTitlepic:[param objectAtIndex:2]];
    [obj setBigpic:[param objectAtIndex:3]];
    [obj setIsgood:[param objectAtIndex:4]];
    [obj setFirsttitle:[param objectAtIndex:5]];
    if (![context save:&error]) {
        NSLog(@"the error is %@",[error userInfo]);
    }else{
        NSLog(@"insert successfully!");
        //NSLog (@"The newManagedObject's id is: %@", [obj valueForKey:@"title"]);
    }
}
-(void)insert{
    IndexData *obj=(IndexData *)[NSEntityDescription insertNewObjectForEntityForName:tblname inManagedObjectContext:context];
    [obj setClassid:@"3189"];
    [obj setTitle:@"梦立方"];
    [obj setTitlepic:@"http://static.statickksmg.com/image/2012/05/15/4320ec4fe15f71a49a94777129abd0f5.jpg"];
    if (![context save:&error]) {
        NSLog(@"the error is %@",[error userInfo]);
    }else{
        NSLog (@"The newManagedObject's id is: %@", [obj valueForKey:@"title"]);
    }
}
-(NSUInteger)count{
    return objects.count;
}
-(void)insertConfig:(NSString *)_isfirst{
    for (ConfigData* _data in [self queryConfig]) {
        [context deleteObject:_data];
    }
    ConfigData *obj=(ConfigData *)[NSEntityDescription insertNewObjectForEntityForName:@"ConfigData" inManagedObjectContext:context];
    [obj setIsfirst:_isfirst];
    [obj setToken:@""];
    [obj setName:@""];
    [obj setUserid:@""];
    if (![context save:&error]) {
        NSLog(@"the error is %@",[error userInfo]);
    }else{
        NSLog(@"insert successfully!");
        //NSLog (@"The newManagedObject's id is: %@", [obj valueForKey:@"title"]);
    }
}
-(void)updateConfig:(NSString *)_token withUserid:(NSString *)_userid withName:(NSString *)_name{
    for (ConfigData* _data in [self queryConfig]) {
        [_data setToken:_token];
        [_data setUserid:_userid];
        [_data setName:_name];
    }
    if (![context save:&error]) {
        NSLog(@"the error is %@",[error userInfo]);
    }else{
        NSLog(@"insert successfully!");
        //NSLog (@"The newManagedObject's id is: %@", [obj valueForKey:@"title"]);
    }
}
-(NSArray*)queryConfig{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"ConfigData" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    objects = [context executeFetchRequest: fetchRequest error:&error];
    return objects;
}
-(NSArray*)query{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:tblname inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"firsttitle" ascending:NO];
    NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"isgood" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1,sortDescriptor2,nil];
    [fetchRequest setSortDescriptors:sortDescriptors];
    //[fetchRequest setFetchBatchSize:20];
    [sortDescriptors release];
    [sortDescriptor1 release];
    [sortDescriptor2 release];
    //[sortDescriptor3 release];
    objects = [context executeFetchRequest: fetchRequest error:&error];
    
    //NSLog(@"%d",objects.retainCount);
    return objects;
}
-(void)delall{
    //[context deleteObject:];
    /*for (IndexData *oneObject in [self query])
    {
        [context deleteObject:oneObject];
    }*/
    for (VideoListData *_data in [self queryVideoList:@"all"]) {
        [context deleteObject:_data];
    }
    if (![context save:&error]) {
        NSLog(@"the error is %@",[error userInfo]);
    }else{
        //NSLog (@"The newManagedObject's id is: %@", [obj valueForKey:@"title"]);
    }
    [delegate processComplete];
}
@end
